

//
//  PersonalCenterTVC.m
//  qiuShiJiangHu
//
//  Created by xalo on 16/1/25.
//  Copyright © 2016年 糗事江湖项目组. All rights reserved.
//

#import "PersonalCenterTVC.h"
#import "LoginHelper.h"
#import "LoginViewController.h"
#import "PrivateLetterTableViewController.h"
#import "DataBaseHelper.h"
@interface PersonalCenterTVC ()

@end

@implementation PersonalCenterTVC
static BOOL isNight=NO;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"640x130"] forBarMetrics:(UIBarMetricsDefault)];
    
    self.navigationController.navigationBar.translucent = NO;   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==0) {
        return 1;
    }else{
    return 3;
    }
}
-(void)viewWillAppear:(BOOL)animated{
    [self.tableView reloadData];

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell4" forIndexPath:indexPath];
    LoginHelper *login=[LoginHelper sharedManager];
               
            if (indexPath.section==0) {
                
        if (login.isLogin==YES) {
            NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
           NSString *userName=[user objectForKey:@"userName"];
            cell.imageView.image=[UIImage imageNamed:@"用户头像"];
            cell.imageView.layer.cornerRadius=32;
            cell.imageView.layer.masksToBounds=YES;
            cell.textLabel.text=userName;
            cell.userInteractionEnabled=NO;
            
        }else {
        cell.imageView.image=[UIImage imageNamed:@"Icon@2x"];
            cell.textLabel.text=@"登录/注册";
        }
    }else if (indexPath.row==0){
    
    cell.textLabel.text=@"我的收藏";
    
    }else if (indexPath.row==1){
    cell.textLabel.text=@"一键换肤";
    
    }else{
    cell.textLabel.text=@"清除收藏";
    
    }
       
    return cell;
}
    
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        LoginViewController *loginVC=[[UIStoryboard storyboardWithName:@"yan" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginViewController"];
        [self presentViewController:loginVC animated:YES completion:nil];
        loginVC.block=^(){
            
        };
    }else if(indexPath.section==1 && indexPath.row==0){
        if ([LoginHelper sharedManager].isLogin) {
            PrivateLetterTableViewController *collectVC=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"PrivatelLetterTVC"];
            [self.navigationController pushViewController:collectVC animated:YES];
        }else{
            LoginViewController *loginVC = [[UIStoryboard storyboardWithName:@"yan" bundle:nil]instantiateViewControllerWithIdentifier:@"LoginViewController"];
            [self presentViewController:loginVC animated:YES completion:nil];
            loginVC.block=^(){
                PrivateLetterTableViewController *collectVC=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"PrivatelLetterTVC"];
                [self.navigationController pushViewController:collectVC animated:YES];
            };
        }
       
    }else if(indexPath.section==1 && indexPath.row==1){

        if (!isNight) {
             [[NSNotificationCenter defaultCenter] postNotificationName:kNight object:self userInfo:nil];
            isNight = YES;
            
        }else {
 
            [[NSNotificationCenter defaultCenter] postNotificationName:kLight object:self userInfo:nil];
            isNight=NO;
        }
        
        [self.tableView reloadData];
    }else
    {
        [[DataBaseHelper sharedManager] deleteAll];
        [self.tableView reloadData];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0 && indexPath.row==0) {
        return 100;
    }else {
        return 50;
    }


}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
